﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Net;
using System.Text;

/// <summary>
/// Author: Dan Miller
/// Pulls the ZPid value from CyberHomes.com's API using the address, 
/// then uses the ZPid to get the Estimate from the API.
/// </summary>
public class CyberHomesEstimate
{
    public String GetCHestimate(String address)
    {
        // used for building CHEstimate
        StringBuilder sb = new StringBuilder();
        // used for query string
        StringBuilder sb2 = new StringBuilder();

        // build query string
        char[] chArr = new char[150];
        chArr = address.ToCharArray();
        String tempStr;
        int count = 0;
        tempStr = chArr[count].ToString();
        while (tempStr != ",")
        {
            if (tempStr == " ")
            {
                sb2.Append("+");
            }
            else
            {
                sb2.Append(tempStr);
            }
            count++;
            tempStr = chArr[count].ToString();
        }

        sb2.Append("&citystatezip=");

        while (count < address.Length - 1)
        {
            count++;
            tempStr = chArr[count].ToString();
            if (tempStr == ",")
            {
                sb2.Append("%2C");
            }
            else if (tempStr == " ")
            {
                sb2.Append("+");
            }
            else
            {
                sb2.Append(tempStr);
            }
        }
        String qStrAddress = sb2.ToString();

        byte[] buf = new byte[1];
        HttpWebRequest request = (HttpWebRequest)
        WebRequest.Create("http://api.cyberhomes.com//valuation.asmx/GetPropertyDetail?streetAddress=" + qStrAddress + "&detailLevel=0&username=DMiller221&password=51D6CC78-200B-4146-AD25-F4AE2CCD3EAC&cyberhomesUserID=");
        HttpWebResponse response = (HttpWebResponse)
        request.GetResponse();
        Stream resStream = response.GetResponseStream();
        string tempString = "";
        bool foundZPID = false;
        count = 0;

        while (foundZPID == false)
        {
            // get byte for one character
            count = resStream.Read(buf, 0, buf.Length);
            if (count == 0)
            {
                return "n/a";
            }

            // translate from bytes to ASCII text
            tempString = Encoding.ASCII.GetString(buf, 0, count);
            // find zpid tag
            if (tempString == "E")
            {
                count = resStream.Read(buf, 0, buf.Length);
                tempString = Encoding.ASCII.GetString(buf, 0, count);
                if (tempString == "s")
                {
                    count = resStream.Read(buf, 0, buf.Length);
                    tempString = Encoding.ASCII.GetString(buf, 0, count);
                    if (tempString == "t")
                    {
                        count = resStream.Read(buf, 0, buf.Length);
                        tempString = Encoding.ASCII.GetString(buf, 0, count);
                        if (tempString == "i")
                        {
                            count = resStream.Read(buf, 0, buf.Length);
                            tempString = Encoding.ASCII.GetString(buf, 0, count);
                            if (tempString == "m")
                            {
                                count = resStream.Read(buf, 0, buf.Length);
                                tempString = Encoding.ASCII.GetString(buf, 0, count);
                                if (tempString == "a")
                                {
                                    count = resStream.Read(buf, 0, buf.Length);
                                    tempString = Encoding.ASCII.GetString(buf, 0, count);
                                    if (tempString == "t")
                                    {
                                        count = resStream.Read(buf, 0, buf.Length);
                                        tempString = Encoding.ASCII.GetString(buf, 0, count);
                                        if (tempString == "e")
                                        {
                                            count = resStream.Read(buf, 0, buf.Length);
                                            tempString = Encoding.ASCII.GetString(buf, 0, count);
                                            if (tempString == "V")
                                            {
                                                count = resStream.Read(buf, 0, buf.Length);
                                                tempString = Encoding.ASCII.GetString(buf, 0, count);
                                                if (tempString == "a")
                                                {
                                                    count = resStream.Read(buf, 0, buf.Length);
                                                    tempString = Encoding.ASCII.GetString(buf, 0, count);
                                                    if (tempString == "l")
                                                    {
                                                        count = resStream.Read(buf, 0, buf.Length);
                                                        tempString = Encoding.ASCII.GetString(buf, 0, count);
                                                        if (tempString == "u")
                                                        {
                                                            count = resStream.Read(buf, 0, buf.Length);
                                                            tempString = Encoding.ASCII.GetString(buf, 0, count);
                                                            if (tempString == "e")
                                                            {
                                                                // get rid of extra character
                                                                count = resStream.Read(buf, 0, buf.Length);
                                                                count = resStream.Read(buf, 0, buf.Length);
                                                                tempString = Encoding.ASCII.GetString(buf, 0, count);
                                                                // build zpid value
                                                                while (tempString != "<")
                                                                {
                                                                    sb.Append(tempString);
                                                                    count = resStream.Read(buf, 0, buf.Length);
                                                                    tempString = Encoding.ASCII.GetString(buf, 0, count);
                                                                }
                                                                foundZPID = true;
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return sb.ToString();
    }
}